<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Session;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public $request;
    public $response;
    public function __construct(Request $request)
    {
        $this->request  = $request;
        $this->response = response();
    }

    /**
     * auth
     * @return bool
     */
    public function getAuth() {
        $mobile = Session::get('auth-mobile');
        if($mobile && in_array($mobile, config('auth.mobile'))) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否是POST请求
     * @return bool
     */
    public function isPost()
    {
        return strcasecmp($this->request->getMethod(), 'post') === 0;
    }

    /**
     * 判断是否是get请求
     * @return bool
     */
    public function isGet()
    {
        return strcasecmp($this->request->getMethod(), 'get') === 0;
    }

    /**
     * 输出 Json 信息
     * @param $status
     * @param null $data
     * @param null $message
     * @return $this|Response
     */
    public function setJson($status, $message = null, $data = null)
    {
        $content = ['status' => $status, 'msg' => $message];
        if (empty($data) === false) {
            $content['data'] = $data;
        }
        $content['time']   = time();
        $this->response = $this->response->json($content)
            ->header('Pragma', 'no-cache')
            ->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        return $this->response;
    }

    /**
     * layui 专用
     * @param $code
     * @param $msg
     * @param $count
     * @param $data
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function setLayuiJson($code, $msg, $count, $data) {
        $content = [
            'code' => $code,
            'msg' => $msg,
            'count' => $count,
            'data' => $data,
        ];
        $content['_']   = time();
        $this->response = $this->response->json($content)
            ->header('Pragma', 'no-cache')
            ->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        return $this->response;
    }
}
